<template>
  <div class="app-container">
    <div class="head-container">
      <el-input v-model="userName" clearable placeholder="输入学员姓名搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
    </div>
    <teacher-user-rel ref="teachUser"/>
    <el-table v-loading="loading" :data="data" ref="table" size="small" style="width: 100%;" border>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60">
      </el-table-column>
      <el-table-column prop="name" label="学员姓名" align="center"/>
      <el-table-column prop="username" label="学员账号"  align="center"/>
      <el-table-column prop="phone" label="手机号"  align="center"/>
      <el-table-column prop="id" label="所属导师" align="center">
        <template slot-scope="scope">
          <span>{{ getTeachName(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import teacherUserRel from './teacherUserRel'

  export default {
    components: { teacherUserRel },
    mixins: [initData],
    data() {
      return {
        loading: false,
        delLoading: false,
        userName: '',
        roleCode: 'r_student'
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      beforeInit() {
        this.url = 'web/user/page-list-rel'
        this.params = {
          page: this.page,
          size: this.size,
          roleCode: this.roleCode
        }
        return true
      },
      getTeachName(row) {
        if (row.teacher) {
          return row.teacher.map((item) => item.name).join(',')
        }
        return ''
      },
      edit(row) {
        let teacherId = []
        if (row.teacher) {
          teacherId = row.teacher.map((item) => item.id)
        }
        this.$refs.teachUser.userRel = {
          userId: row.id,
          userName: row.name,
          teacherId: teacherId
        }
        this.$refs.teachUser.getTeacherList()
        this.$refs.teachUser.dialog = true
      }
    }
  }
</script>

<style scoped>

</style>
